package com.scoy.honeymei.activity.me;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.AddressBean;
import com.scoy.honeymei.bean.CityBean;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.CheckUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.LinkagePicker;

/**
 * @ClassName: AddAdressActivity$
 * @Description: 添加收货地址
 * @Author: GaoZhan
 * @CreateDate: 2019/9/5/0005$ 下午 15:04$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/5/0005$ 下午 15:04$
 * @UpdateRemark: 更新内容
 */
public class AddAdressActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.aet_name_et)
    EditText aetNameEt;
    @BindView(R.id.aet_phone_et)
    EditText aetPhoneEt;
    @BindView(R.id.aet_checksyq_tv)
    TextView aetChecksyqTv;
    @BindView(R.id.aet_detail_et)
    EditText aetDetailEt;
    @BindView(R.id.aet_save_tv)
    TextView aetSaveTv;
    private AddAdressActivity mContext;
    private int type;
    private int cityId;  //省市区id
    private int addressId; //地址id

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_edit);
        ButterKnife.bind(this);
        mContext = AddAdressActivity.this;
        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {
            AddressBean addressBean = (AddressBean) getIntent().getParcelableExtra("addressBean");
            addressId = addressBean.getId();
            aetNameEt.setText(addressBean.getUsername());
            aetPhoneEt.setText(addressBean.getPhone());
            aetChecksyqTv.setText(addressBean.getArea());
            aetDetailEt.setText(addressBean.getAddressdetail());
            cityId = addressBean.getAreaid();
        }

        initNormal();

    }

    @Override
    public void initNormal() {

        titleTv.setText(R.string.addreciveaddress);
        if (type == 1) {
            titleTv.setText(R.string.editaddress);
        }

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

    }

    @OnClick({R.id.back_iv, R.id.aet_checksyq_tv, R.id.aet_save_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.aet_checksyq_tv:
                httpPart();
                break;
            case R.id.aet_save_tv:
                preclick();
                break;
        }
    }

    private void preclick() {
        if (TextUtils.isEmpty(aetNameEt.getText())) {
            MyUtil.mytoast(mContext, "请输入姓名");
            return;
        }
        if (TextUtils.isEmpty(aetPhoneEt.getText())) {
            MyUtil.mytoast(mContext, "请输入姓名");
            return;
        }
        if (!CheckUtil.isMobileNO(aetPhoneEt.getText().toString())) {
            MyUtil.mytoast(mContext, "请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(aetChecksyqTv.getText())) {
            MyUtil.mytoast(mContext, "请选择地址");
            return;
        }
        if (TextUtils.isEmpty(aetDetailEt.getText())) {
            MyUtil.mytoast(mContext, "请输入详细地址");
            return;
        }

        httpAddAddressList();

    }

    private void httpAddAddressList() {
        HttpParams params = new HttpParams();
        params.put("userid", SPHelper.getString(MeConstant.UID, ""));
        params.put("areaid", cityId);
        params.put("addressdetail", aetDetailEt.getText().toString());
        params.put("username", aetNameEt.getText().toString());
        params.put("phone", aetPhoneEt.getText().toString());
        if (type == 1) {
            params.put("id", addressId);
        }

        HpGo.newInstance().HttpGo(mContext, type == 1 ? MyUrl.EDIT_ADDERSS : MyUrl.ADD_ADDRESS, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                setResult(0x114);
                MyUtil.mytoast(mContext, "成功");
                finish();
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    //获取省市区列表
    private void httpPart() {
        HttpParams params = new HttpParams();
        HpGo.newInstance().HttpGo(mContext, MyUrl.GET_AREA_LIST, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                List<CityBean> data = new Gson().fromJson(o, new TypeToken<List<CityBean>>() {
                }.getType());
                ArrayList<CityBean> data0 = new ArrayList<>();
                data0.addAll(data);
                checkPart(data0);
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }

    //选择省市区
    private void checkPart(ArrayList<CityBean> data0) {
        //联动选择器的更多用法，可参见AddressPicker和CarNumberPicker
        LinkagePicker.DataProvider provider = new LinkagePicker.DataProvider() {

            @Override
            public boolean isOnlyTwo() {
                return false;
            }

            @NonNull
            @Override
            public List<String> provideFirstData() {
                ArrayList<String> firstList = new ArrayList<>();
                for (CityBean iBean : data0) {
                    firstList.add(iBean.getName());
                }
                return firstList;
            }

            @NonNull
            @Override
            public List<String> provideSecondData(int firstIndex) {
                ArrayList<String> secondList = new ArrayList<>();
                CityBean data1list = data0.get(firstIndex);
                List<CityBean> data1 = data1list.getChild();
                for (CityBean iBean : data1) {
                    secondList.add(iBean.getName());
                }
                return secondList;
            }

            @Nullable
            @Override
            public List<String> provideThirdData(int firstIndex, int secondIndex) {
                ArrayList<String> thirdList = new ArrayList<>();
                CityBean cityBean = data0.get(firstIndex).getChild().get(secondIndex);
                List<CityBean> data2 = cityBean.getChild();
                for (CityBean iBean : data2) {
                    thirdList.add(iBean.getName());
                }
                return thirdList;
            }

        };
        LinkagePicker picker = new LinkagePicker(this, provider);
        picker.setCycleDisable(true);
        picker.setUseWeight(true);
        picker.setSelectedIndex(0, 0, 0);
        picker.setContentPadding(10, 0);
        picker.setOnStringPickListener(new LinkagePicker.OnStringPickListener() {
            @Override
            public void onPicked(String first, String second, String third) {
                aetChecksyqTv.setText(first + "-" + second + "-" + third);

                int selectedFirstIndex = picker.getSelectedFirstIndex();
                int selectedSecondIndex = picker.getSelectedSecondIndex();
                int selectedThirdIndex = picker.getSelectedThirdIndex();
                CityBean cityBean = data0.get(selectedFirstIndex).getChild().get(selectedSecondIndex).getChild().get(selectedThirdIndex);
                cityId = cityBean.getId();
            }
        });
        picker.show();
    }

}
